package com.edu.chat.web.shared;

import com.edu.buservice.util.model.TenantUtil;
import com.edu.chat.facade.facade.MessageFacade;
import com.edu.chat.facade.model.dto.HistoryMsgDTO;
import com.edu.chat.facade.model.dto.SendMessageDTO;
import com.edu.chat.web.service.CsMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-23 18:37
 **/

@DubboService(interfaceClass = MessageFacade.class)
@Service
@Slf4j
public class MessageFacadeImpl implements MessageFacade {

	@Autowired
	private CsMessageService csMessageService;

	@Override
	public List<HistoryMsgDTO> queryMsgList(String sessionId, String tenantId) {

		try{
			TenantUtil.setCurrentTenantId(tenantId);

			return csMessageService.queryMsgList(sessionId);

		}catch (Exception e){
			log.info("queryMsgList error:{}",e.getMessage());
			return List.of();
		}finally {
			TenantUtil.clear();
		}
	}

	@Override
	public void addMessage(SendMessageDTO sendMessageDTO) {

		try{
			TenantUtil.setCurrentTenantId(sendMessageDTO.getTenantId());
			TenantUtil.setCurrentUserId(sendMessageDTO.getUserId());

			csMessageService.insertMsg(sendMessageDTO);

		}catch (Exception e){
			log.info("addMessage error:{}",e.getMessage());
			return ;
		}finally {
			TenantUtil.clear();
		}

	}
}
